﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Configuration;

namespace Gmis.DalFactory
{
    /// <summary>
    /// 抽象工厂模式创建DAL。
    /// web.config 需要加入配置：(利用工厂模式+反射机制+缓存机制,实现动态创建不同的数据层对象接口)  
    /// DataCache类在导出代码的文件夹里
    /// <appSettings>  
    /// <add key="DAL" value="Gmis.SQLServerDAL" /> (这里的命名空间根据实际情况更改为自己项目的命名空间)
    /// </appSettings> 
    /// </summary>
    public sealed class DataAccess
    {
        private static readonly string AssemblyPath = ConfigurationManager.AppSettings["WebDAL"];
        /// <summary>
        /// 创建对象或从缓存获取
        /// </summary>
        public static object CreateObject(string AssemblyPath, string ClassNamespace)
        {
            object objType = DataCache.GetCache(ClassNamespace);//从缓存读取
            if (objType == null)
            {
                try
                {
                    objType = Assembly.Load(AssemblyPath).CreateInstance(ClassNamespace);//反射创建
                    DataCache.SetCache(ClassNamespace, objType);// 写入缓存
                }
                catch
                { }
            }
            return objType;
        }
       
        /// <summary>
        /// 创建Village数据层接口。
        /// </summary>
        public static Gmis.IDAL.IVillage CreateVillage()
        {
            string ClassNamespace = AssemblyPath + ".Village";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Gmis.IDAL.IVillage)objType;
        }

        public static IDAL.IColumn CreateColumn()
        {
            string ClassNamespace = AssemblyPath + ".Column";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Gmis.IDAL.IColumn)objType;
        }

        /// <summary>
        /// 创建PMember数据层接口。
        /// </summary>
        public static Gmis.IDAL.IPMember CreatePMember()
        {
            string ClassNamespace = AssemblyPath + ".PMember";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Gmis.IDAL.IPMember)objType;
        }
        /// <summary>
        /// 创建NewsFile数据层接口。
        /// </summary>
        public static Gmis.IDAL.INewsFile CreateNewsFile()
        {
            string ClassNamespace = AssemblyPath + ".NewsFile";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Gmis.IDAL.INewsFile)objType;
        }

        /// <summary>
        /// 创建PPreMember数据层接口。
        /// </summary>
        public static Gmis.IDAL.IPPreMember CreatePPreMember()
        {
            string ClassNamespace = AssemblyPath + ".PPreMember";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Gmis.IDAL.IPPreMember)objType;
        }

        /// <summary>
        /// 创建PMemberChange数据层接口。
        /// </summary>
        public static Gmis.IDAL.IPMemberChange CreatePMemberChange()
        {

            string ClassNamespace = AssemblyPath + ".PMemberChange";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Gmis.IDAL.IPMemberChange)objType;
        }

        /// <summary>
        /// 创建Affairs数据层接口。
        /// </summary>
        public static Gmis.IDAL.IAffairs CreateAffairs()
        {

            string ClassNamespace = AssemblyPath + ".Affairs";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Gmis.IDAL.IAffairs)objType;
        }

        /// <summary>
        /// 创建Document数据层接口。
        /// </summary>
        public static Gmis.IDAL.IDocument CreateDocument()
        {

            string ClassNamespace = AssemblyPath + ".Document";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Gmis.IDAL.IDocument)objType;
        }

        public static IDAL.INews CreateNews()
        {
            string ClassNamespace = AssemblyPath + ".News";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Gmis.IDAL.INews)objType;
        }

        public static IDAL.IFOpen CreateFOpen()
        {
            string ClassNamespace = AssemblyPath + ".FOpen";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Gmis.IDAL.IFOpen)objType;
        }

        public static IDAL.IFAccount CreateFAccount()
        {
            string ClassNamespace = AssemblyPath + ".FAccount";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Gmis.IDAL.IFAccount)objType;
        }

        /// <summary>
        /// 创建SocialAssistance数据层接口。
        /// </summary>
        public static Gmis.IDAL.ISocialAssistance CreateSocialAssistance()
        {

            string ClassNamespace = AssemblyPath + ".SocialAssistance";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Gmis.IDAL.ISocialAssistance)objType;
        }

        /// <summary>
        /// 创建LogUser数据层接口。
        /// </summary>
        public static Gmis.IDAL.ILogUser CreateLogUser()
        {

            string ClassNamespace = AssemblyPath + ".LogUser";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Gmis.IDAL.ILogUser)objType;
        }

        /// <summary>
        /// 创建Population数据层接口。
        /// </summary>
        public static Gmis.IDAL.IPopulation CreatePopulation()
        {

            string ClassNamespace = AssemblyPath + ".Population";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Gmis.IDAL.IPopulation)objType;
        }

        /// <summary>
        /// 创建Branch数据层接口。
        /// </summary>
        public static Gmis.IDAL.IBranch CreateBranch()
        {
            string ClassNamespace = AssemblyPath + ".Branch";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Gmis.IDAL.IBranch)objType;
        }

        /// <summary>
        /// 创建Committee数据层接口。
        /// </summary>
        public static Gmis.IDAL.ICommittee CreateCommittee()
        {

            string ClassNamespace = AssemblyPath + ".Committee";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Gmis.IDAL.ICommittee)objType;
        }

        /// <summary>
        /// 创建VSenado数据层接口。
        /// </summary>
        public static Gmis.IDAL.IVSenado CreateVSenado()
        {

            string ClassNamespace = AssemblyPath + ".VSenado";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Gmis.IDAL.IVSenado)objType;
        }

        /// <summary>
        /// 创建DSenado数据层接口。
        /// </summary>
        public static Gmis.IDAL.IDSenado CreateDSenado()
        {

            string ClassNamespace = AssemblyPath + ".DSenado";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Gmis.IDAL.IDSenado)objType;
        }

        /// <summary>
        /// 创建VSupervise数据层接口。
        /// </summary>
        public static Gmis.IDAL.IVSupervise CreateVSupervise()
        {

            string ClassNamespace = AssemblyPath + ".VSupervise";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Gmis.IDAL.IVSupervise)objType;
        }

        /// <summary>
        /// 创建FSupervise数据层接口。
        /// </summary>
        public static Gmis.IDAL.IFSupervise CreateFSupervise()
        {

            string ClassNamespace = AssemblyPath + ".FSupervise";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Gmis.IDAL.IFSupervise)objType;
        }


        /// <summary>
        /// 创建VSystem数据层接口。
        /// </summary>
        public static Gmis.IDAL.IVSystem CreateVSystem()
        {
            string ClassNamespace = AssemblyPath + ".VSystem";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Gmis.IDAL.IVSystem)objType;
        }

        /// <summary>
        /// 创建PSystem数据层接口。
        /// </summary>
        public static Gmis.IDAL.IPSystem CreatePSystem()
        {
            string ClassNamespace = AssemblyPath + ".PSystem";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Gmis.IDAL.IPSystem)objType;
        }

        /// <summary>
        /// 创建MassOrg数据层接口。
        /// </summary>
        public static Gmis.IDAL.IMassOrg CreateMassOrg()
        {
            string ClassNamespace = AssemblyPath + ".MassOrg";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Gmis.IDAL.IMassOrg)objType;
        }

        /// <summary>
        /// 创建PActivist数据层接口。
        /// </summary>
        public static Gmis.IDAL.IPActivist CreatePActivist()
        {
            string ClassNamespace = AssemblyPath + ".PActivist";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Gmis.IDAL.IPActivist)objType;
        }

        /// <summary>
        /// 创建Artel数据层接口。
        /// </summary>
        public static Gmis.IDAL.IArtel CreateArtel()
        {
            string ClassNamespace = AssemblyPath + ".Artel";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Gmis.IDAL.IArtel)objType;
        }

        /// <summary>
        /// 创建ArtelMember数据层接口。
        /// </summary>
        public static Gmis.IDAL.IArtelMember CreateArtelMember()
        {
            string ClassNamespace = AssemblyPath + ".ArtelMember";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Gmis.IDAL.IArtelMember)objType;
        }

        /// <summary>
        /// 创建ArtelBuy数据层接口。
        /// </summary>
        public static Gmis.IDAL.IArtelBuy CreateArtelBuy()
        {
            string ClassNamespace = AssemblyPath + ".ArtelBuy";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Gmis.IDAL.IArtelBuy)objType;
        }
        /// <summary>
        /// 创建ArtelSell数据层接口。
        /// </summary>
        public static Gmis.IDAL.IArtelSell CreateArtelSell()
        {
            string ClassNamespace = AssemblyPath + ".ArtelSell";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Gmis.IDAL.IArtelSell)objType;
        }
        public static IDAL.ILeaveWord CreateLeaveWord()
        {
            string ClassNamespace = AssemblyPath + ".LeaveWord";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Gmis.IDAL.ILeaveWord)objType;
        }
    }
}
